package com.intco.ait.quotation.api.infrastructure.mybatis.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intco.ait.quotation.api.infrastructure.mybatis.entity.encodingPlatform.Material;
import com.intco.ait.quotation.api.infrastructure.mybatis.entity.encodingPlatform.ProductAttribute;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@DS("nc")
public interface EncodingPlatformMapper {

    @Select({
            "<script>",
            "SELECT",
            "  a.code AS materialCode,",
            "  a.NAME AS materialName,",
            "  c.user_name AS creator,",
            "  a.CREATIONTIME AS createTime,",
            "  b.code AS materialType,",
            "  b.name AS productClass,",
            "  pa.SUPPLIER_PRODUCT_MODEL",
            "  <if test='productBusiness != null and productBusiness == \"building\"'>",
            "    ,CASE WHEN ISNUMERIC(a.def3)=1 THEN a.def3 ELSE '0' END AS length,",
            "    CASE WHEN ISNUMERIC(a.def1)=1 THEN a.def1 ELSE '0' END AS boxPackingCount",
            "  </if>",
            "FROM bd_material_v a",
            "LEFT JOIN bd_marbasclass b ON b.PK_MARBASCLASS = a.PK_MARBASCLASS",
            "LEFT JOIN sm_user c ON a.CREATOR = c.cuserid",
            "LEFT JOIN PRODUCT_ATTRIBUTE pa ON a.CODE = pa.MATERIAL_CODE",
            "WHERE a.ENABLESTATE = '2' AND a.DR = '0'",
            "<bind name='startCodeStr' value='startCode != null ? startCode + \"%\" : \"\"'/>",
            "<if test='startCode == \"50\"'>",
            "  AND (a.code LIKE #{startCodeStr} OR a.code LIKE '0708%')",
            "</if>",
            "<if test='startCode == \"07\"'>",
            "  AND (a.code LIKE #{startCodeStr}",
            "       AND a.code NOT LIKE '0708%'",
            "       AND a.code NOT LIKE '0710%'",
            "       AND a.code NOT LIKE '0713%'",
            "       AND a.code NOT LIKE '0714%'",
            "       AND a.code NOT LIKE '0716%'",
            "       AND a.code NOT LIKE '0717%'",
            "       AND a.code NOT LIKE '0718%')",
            "</if>",
            "<if test='startCode == \"FRAME\"'>",
            "  AND (a.code LIKE '05%'",
            "       OR a.code LIKE '090202%'",
            "       OR a.code LIKE '090102%'",
            "       OR a.code LIKE '1601%'",
            "       OR a.code LIKE '1602%')",
            "</if>",
            "<if test='buildingType == \"CP\"'>",
            "  AND (a.code LIKE '0708%'",
            "       OR a.code LIKE '0710%'",
            "       OR a.code LIKE '0713%'",
            "       OR a.code LIKE '0714%'",
            "       OR a.code LIKE '0716%'",
            "       OR a.code LIKE '0717%'",
            "       OR a.code LIKE '0718%')",
            "</if>",
            "<if test='buildingType == \"KT\"'>",
            "  AND a.code LIKE '50%'",
            "</if>",
            "<if test='materialCode != null and materialCode != \"\"'>",
            "  <bind name='searchParam' value=\"'%' + materialCode + '%'\"/>",
            "  AND (a.code LIKE #{searchParam} OR a.name LIKE #{searchParam})",
            "</if>",
            "<if test='creator != null and creator != \"\"'>",
            "  AND a.creator = #{creator}",
            "</if>",
            "<if test='startTime != null and startTime != \"\"'>",
            "  AND a.creationtime &gt;= #{startTime}",
            "</if>",
            "<if test='endTime != null and endTime != \"\"'>",
            "  AND #{endTime} &gt;= a.creationtime",
            "</if>",
            "<if test='outsource != null'>",
            "  AND pa.OUTSOURCE = #{outsource}",
            "</if>",
            "ORDER BY a.creationtime DESC",
            "</script>"
    })
    IPage<Material> getMaterialList(Page<Material> page,
                                    @Param("productBusiness") String productBusiness,
                                    @Param("materialCode") String materialCode,
                                    @Param("startCode") String startCode,
                                    @Param("startTime") String startTime,
                                    @Param("endTime") String endTime,
                                    @Param("creator") String creator,
                                    @Param("buildingType") String buildingType,
                                    @Param("outsource") Integer outsource);


    @Select({
            "SELECT",
            "  a.code AS materialCode,",
            "  a.NAME AS materialName,",
            "  b.code AS materialType,",
            "  b.name AS productClass,",
            "  a.MATERIALSPEC AS buildingSize,",
            "  c.FEATURES AS productFun,",
            "  c.PRODUCT_SHAPE AS materialShape,",
            "  c.SIZE_INCH AS innerSizeInch,",
            "  c.SIZE_MM AS innerSizeMm,",
            "  c.FRAME_SURFACE_TECHNOLOGY AS surfaceTechnology,",
            "  c.CUSTOMS_DECLARATION_MARK AS declareFlagName,",
            "  c.MIRROR_EDGE_GRINDING AS edgeGrinding,",
            "  c.FRAME_CRAFTSMANSHIP AS frameTechnology,",
            "  c.FRAME_SOURCE AS pictureSource,",
            "  c.FRAME_THEME AS frameTheme,",
            "  c.ODMCODE AS odmCode,",
            "  c.SAMPLE_DETAIL_ID AS frameSampleNo,",
            "  c.SPECIAL AS productFeatures,",
            "  c.PROCESS_TYPE AS processType,",
            "  c.CUST_SHORT AS cusShortName,",
            "  CASE WHEN c.CUST_ITEM is NULL THEN a.DEF6 ELSE c.CUST_ITEM END AS cusItem,",
            "  CASE WHEN c.MOULD is NULL THEN a.DEF4 ELSE c.MOULD END AS mould,",
            "  c.BOX_NUM AS boxNum,",
            "  c.UNIT_NUM AS unitNum,",
            "  c.PCS_PERTAO AS pcsPerTao,",
            "  c.REMARKS AS remarks,",
            "  c.MAKINGS AS materialQuality,",
            "  CASE WHEN ISNUMERIC(a.def3)=1 THEN a.def3 ELSE '0' END AS length,",
            "  CASE WHEN ISNUMERIC(a.def1)=1 THEN a.def1 ELSE '0' END AS boxPackingCount,",
            "  c.PRODUCT_CATEGORIES,",
            "  c.MOULD_WIDTH,",
            "  c.MOULD_HEIGHT,",
            "  c.MOULD_SECTIONAL_AREA,",
            "  c.MOULD_DENSITY,",
            "  c.MOULD_METER_WEIGHT,",
            "  c.SURFACE_STYLE,",
            "  c.APPLICATION_SCENARIOS",
            "FROM bd_material a",
            "LEFT JOIN bd_marbasclass b ON b.PK_MARBASCLASS = a.PK_MARBASCLASS",
            "LEFT JOIN PRODUCT_ATTRIBUTE c ON a.code = c.MATERIAL_CODE AND c.MATERIAL_CODE = #{materialCode}",
            "WHERE a.code = #{materialCode}"
    })
    Material getMaterialByCode(String materialCode);


    @Select("SELECT * FROM PRODUCT_ATTRIBUTE WHERE MATERIAL_CODE = #{materialCode} AND DR = 0")
    ProductAttribute getProductAttributeByMaterialCode(@Param("materialCode") String materialCode);

}
